package com.tbit.main.controller;

import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.exception.BaseException;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserWorkConfig;
import com.tbit.main.pojo.ClassesConfig;
import com.tbit.main.service.AccountUserWorkConfigService;
import com.tbit.main.service.ClassesConfigService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.LogUtil;
import com.tbit.main.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 班次配置
 *
 * @author LMY
 * @create 2021-08-25 17:02
 */
@RestController
@RequestMapping("/classesConfigController")
public class ClassesConfigController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private ClassesConfigService classesConfigService;
    @Autowired
    private AccountUserWorkConfigService accountUserWorkConfigService;

    /**
     * 班次添加修改
     *
     * @param token
     * @param classesConfig
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.MAINCLASSESCONFIG, methods = MethodEnum.CLASSESCONFIG_ADD)
    public JsonResult add(String token, ClassesConfig classesConfig) {
        Assert.notNull(classesConfig.getAccountId(), "AccountUser.agentId.notNull");
        AccountUser accountUser = tokenService.getAccount(token);

        if (classesConfig.getWorkNumber() == null) {
            Assert.notNull(classesConfig.getStartTime(), "System.startTime.notNull");
            Assert.notNull(classesConfig.getEndTime(), "System.endTime.notNul");
            Assert.notNull(classesConfig.getName(), "Classes.name.notNull");
            Assert.notNull(classesConfig.getAccountId(), "AccountUser.agentId.notNull");
            classesConfig.setAddTime(DateTimeUtil.getNowTime());

            List<ClassesConfig> classesConfigList = classesConfigService.getAccountId(classesConfig.getAccountId());
            for (ClassesConfig classesConfigDB : classesConfigList) {
                if (classesConfig.getName().equals(classesConfigDB.getName())) {
                    throw new BaseException("Classes.name.notIdentical");
                }
            }
            LogUtil.info(accountUser.getAccountUserId() + "添加班次：" + classesConfigList.toString());
            classesConfigService.add(classesConfig);
        } else {
            ClassesConfig classesConfigDB = classesConfigService.getWorkNumber(classesConfig.getWorkNumber());
            /**替换可修改字段*/
            ObjectUtil.assign(classesConfig, classesConfigDB, null);
            classesConfig.setUpdateTime(DateTimeUtil.getNowTime());

            List<ClassesConfig> classesConfigList = classesConfigService.getAccountId(classesConfig.getAccountId());
            for (ClassesConfig classes : classesConfigList) {
                if (!classes.getWorkNumber().equals(classesConfig.getWorkNumber())) {
                    if (classesConfig.getName().equals(classes.getName())) {
                        throw new BaseException("Classes.name.notIdentical");
                    }
                }
            }
            LogUtil.info(accountUser.getAccountUserId() + "修改班次：" + classesConfig.toString());
            classesConfigService.update(classesConfig);
        }
        return JsonResult.succ();
    }

    /**
     * 删除班次
     *
     * @param token
     * @param workNumber
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.MAINCLASSESCONFIG, methods = MethodEnum.CLASSESCONFIG_DEL)
    public JsonResult del(String token, Integer workNumber) {

        Assert.notNull(workNumber, "Classes.workNumber.notNull");

        List<AccountUserWorkConfig> accountUserWorkConfigList = accountUserWorkConfigService.getByWorkNumber(workNumber);

        if (accountUserWorkConfigList != null && accountUserWorkConfigList.size() > 0) {
            throw new BaseException("Classes.name.isUser");
        }

        classesConfigService.del(workNumber);
        return JsonResult.succ();
    }

    /**
     * 查询运营区域班次
     *
     * @param token
     * @param accountId
     * @return
     */
    @RequestMapping("/getAccountId")
    public JsonResult getAccountId(String token, Integer accountId) {

        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        List<ClassesConfig> classesConfigs = classesConfigService.getAccountId(accountId);
        return JsonResult.succ(classesConfigs);
    }

    /**
     * 根据班次查询
     *
     * @param token
     * @param workNumber
     * @return
     */
    @RequestMapping("/getWorkNumber")
    public JsonResult getWorkNumber(String token, Integer workNumber) {

        Assert.notNull(workNumber, "Classes.workNumber.notNull");

        ClassesConfig classesConfig = classesConfigService.getWorkNumber(workNumber);
        return JsonResult.succ(classesConfig);
    }

}
